<?php
namespace App\Models;

use App\Services\CategoryService;
use App\Services\CommonService;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class CategoryUser extends Model
{
    use HasFactory;

    protected $table = 'sp_category_user_2';
    protected $primaryKey = 'id';
    public $timestamps = false;

    //status状态：1启用 2禁用
    const CATEGORY_USER_STATUS_ENABLE = 1;//启用
    const CATEGORY_USER_STATUS_DISABLE = 2;//禁用

    //ischeck审核状态：1已审核 2待审核 3拒绝审核
    const CATEGORY_USER_IS_CHECK = 1;//已审核
    const CATEGORY_USER_NOT_CHECK = 2;//待审核
    const CATEGORY_USER_REFUSE_CHECK = 3;//审核拒绝

    //is_delete是否删除  1是 2否
    const CATEGORY_USER_IS_DELETE = 1;//是
    const CATEGORY_USER_NOT_DELETE = 2;//否

    //品类管理列表
    protected function GetCategoryManagementList($pageSize, $where)
    {
        $select = array('cu.id', 'cu.member_id', 'cu.category_id', 'cu.ischeck', 'cu.create_time', 'cu.check_refuse_type', 'cu.check_refuse_reason', 'c.parentid', 'c.name');
        $data = $this
            ->from('sp_category_user_2 as cu')
            ->leftjoin('sp_category as c', 'c.id', 'cu.category_id')
            ->select($select)
            ->where($where)
            ->orderBy('cu.id', 'desc')
            ->paginate($pageSize);
        $data = $data ? $data->toArray() : array();
        return $data;
    }

    //根据id获取商户分类信息
    protected function GetCategoryUserById($id)
    {
        $where = array(
            'id' => $id,
        );
        $category = $this
            ->where($where)
            ->first();
        $category = $category ? $category->toArray() : [];
        return $category;
    }

    //根据id修改商户分类信息
    protected function editDataById($id, $data)
    {
        $where = array(
            'id' => $id,
        );
        return $data = $this->where($where)->update($data);
    }

    //获取指定商户已申请的品类
    protected function GetApplyCategoryByMemberid($member_id)
    {
        $select = array('c.name');
        $where = array(
            'cu.member_id' => $member_id,
            'cu.ischeck' => 1, //审核状态：1已审核 2未审核 3拒绝审核
            'cu.status' => 1, //状态：1启用 2禁用
            'cu.is_delete' => 2, //是否删除  1是 2否
        );
        $data = $this
            ->from('sp_category_user_2 as cu')
            ->leftjoin('sp_category as c', 'c.id', 'cu.category_id')
            ->select($select)
            ->where($where)
            ->get();
        $data = $data ? $data->toArray() : array();
        return $data;
    }

    //获取指定商户的二级品类
    protected function GetSecondCategoryByMemberid($pid, $member_id)
    {
        $select = array('c.id', 'c.name', 'c.image', 'c.parentid');
        $where = array(
            'c.enabled' => 1, //状态 1显示 0隐藏
            'c.is_delete' => 2, //是否删除  1是 2否
            'c.parentid' => $pid,
            'cu.member_id' => $member_id,
            'cu.status' => 1, //状态：1启用 2禁用
            'cu.is_delete' => 2, //是否删除  1是 2否
        );
        $data = $this
            ->from('sp_category_user_2 as cu')
            ->leftjoin('sp_category as c', 'c.id', 'cu.category_id')
            ->select($select)
            ->where($where)
            ->orderBy('c.id', 'desc')
            ->get();
        $data = $data ? $data->toArray() : array();
        return $data;
    }

    //获取商户分类信息
    protected function GetOneCategoryUser($where)
    {
        $category = $this
            ->where($where)
            ->first();
        $category = $category ? $category->toArray() : [];
        return $category;
    }

    //添加商户分类信息
    protected function addData($data)
    {
        return $data = $this->insert($data);
    }

    //获取商户下所有品类
    protected function GetShopCategory($member_id)
    {
        $data = $this->from('sp_category_user_2 as cu')
            ->leftjoin('sp_category as c', 'c.id', 'cu.category_id')
            ->select('cu.category_id', 'c.name')
            ->where([['cu.member_id', $member_id], ['cu.ischeck', CategoryUser::CATEGORY_USER_IS_CHECK],
                ['cu.status', CategoryUser::CATEGORY_USER_STATUS_ENABLE], ['cu.is_delete', CategoryUser::CATEGORY_USER_NOT_DELETE],
                ['c.enabled', Category::CATEGORY_STATUS_DISPLAY], ['c.is_delete', Category::CATEGORY_NOT_DELETE]])
            ->get();
        $data = $data ? $data->toArray() : [];
        return $data;
    }

    //品类审核列表
    protected function GetCategoryExamineList($pageSize, $where, $check_where)
    {
        $select = array('cu.id', 'cu.member_id','cu.is_preposition', 'cu.category_id', 'cu.member_id', 'cu.ischeck', 'cu.create_time', 'c.parentid', 'c.name', 'su.shopname');
        $data = $this
            ->from('sp_category_user_2 as cu')
            ->leftjoin('sp_category as c', 'c.id', 'cu.category_id')
            ->leftjoin('sp_shop_user_2 as su', 'su.member_id', 'cu.member_id')
            ->select($select)
            ->where($where)
            ->where($check_where)
            ->orderBy('cu.id', 'desc')
            ->paginate($pageSize);
        $data = $data ? $data->toArray() : array();
        return $data;
    }

    //品类审核状态数量
    protected function GetCategoryExamineIscheckCount($where, $check_where)
    {
        $data = $this
            ->from('sp_category_user_2 as cu')
            ->leftjoin('sp_category as c', 'c.id', 'cu.category_id')
            ->leftjoin('sp_shop_user_2 as su', 'su.member_id', 'cu.member_id')
            ->where($where)
            ->where($check_where)
            ->count();
        return $data;
    }


}
